package org.ss.psci.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.ss.psci.entity.PmAssignment;
import org.ss.psci.entity.PmUser;

/**
 * Data access object (DAO) for domain model class PmUser.
 * 
 * @see org.ss.psci.entity.PmUser
 * @author MyEclipse Persistence Tools
 */

public class PmUserDAO extends HibernateDaoSupport {

	private static final Log log = LogFactory.getLog(PmUserDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String REALNAME = "realname";
	public static final String SEX = "sex";
	public static final String PHONE = "phone";
	public static final String ADDRESS = "address";
	public static final String COMPANY = "company";

	public PmUser copyEntity(PmUser dest, PmUser orig) {
		if (dest == null || orig == null) {
			return new PmUser();
		}
		if (orig.getRealname() != null) {
			dest.setRealname(orig.getRealname());
			dest.setNickname(orig.getRealname());
		}
		if (orig.getAddress() != null) {
			dest.setAddress(orig.getAddress());
		}
		if (orig.getPhone() != null) {
			dest.setPhone(orig.getPhone());
		}
		if (orig.getSex() != null) {
			dest.setSex(orig.getSex());
		}
		if (orig.getCompany() != null) {
			dest.setCompany(orig.getCompany());
		}
		if (orig.getPassword() != null) {
			dest.setPassword(orig.getPassword());
		}
		
		if (orig.getUsername() != null) {
			dest.setUsername(orig.getUsername());
		}
		return dest;
	}

	protected void initDao() {
		// do nothing
	}

	public void save(PmUser transientInstance) {
		log.info("saving PmUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.info("save successful");
		} catch (RuntimeException re) {
			log.info("save failed", re);
			throw re;
		}
	}

	public void delete(PmUser persistentInstance) {
		log.debug("deleting PmUser instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmUser findById(java.lang.Integer id) {
		log.debug("getting PmUser instance with id: " + id);
		try {
			PmUser instance = (PmUser) getHibernateTemplate().get(
					"org.ss.psci.entity.PmUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmUser instance) {
		log.debug("finding PmUser instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmUser as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsernameAndPassword(String username, String password) {
		try {
			String queryString = "from PmUser as model where model.username=? and model.password=?";
			return getHibernateTemplate().find(queryString,
					new Object[] { username, password });
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByRealname(Object realname) {
		return findByProperty(REALNAME, realname);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findAll() {
		log.debug("finding all PmUser instances");
		try {
			String queryString = "from PmUser";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmUser merge(PmUser detachedInstance) {
		log.debug("merging PmUser instance");
		try {
			PmUser result = (PmUser) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmUser instance) {
		log.debug("attaching dirty PmUser instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmUser instance) {
		log.debug("attaching clean PmUser instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmUserDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmUserDAO) ctx.getBean("PmUserDAO");
	}

}